var express = require('express');
var router = express.Router();
// var http = require('http');
var https = require('https');
// var request = require('request');
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://127.0.0.1:27017/project';

router.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header('Access-Control-Allow-Headers', 'X-Requested-With');
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  next();
});

//  获取天气信息
router.get('/weather', function (req, res) {
  var lat = req.query.lat;
  var lng = req.query.lng;
  https.get('https://mainsite-restapi.ele.me/bgs/weather/current?latitude=' + lat + '&longitude=' + lng, function (data) {
    var html = '';
    data.on('data', function (chunk) {
      html += chunk;
    })
    data.on('end', function () {
      res.send(html);
    })
  })
})

//  获取input里面的热门搜索
router.get('/shopping/hot_search_words', function (req, res) {
  var lat = req.query.lat;
  var lng = req.query.lng;
  https.get('https://mainsite-restapi.ele.me/shopping/v3/hot_search_words?geohash=ws103phuv6qcssyx96kzs0&latitude=' + lat + '&longitude=' + lng, function (data) {
    var html = '';
    data.on('data', function (chunk) {
      html += chunk;
    })
    data.on('end', function () {
      res.send(html);
    })
  })
})

//  获取搜索关键字
router.get('/hot_search_words', function (req, res) {
  var lat = req.query.lat;
  var lng = req.query.lng;
  https.get('https://mainsite-restapi.ele.me/shopping/v3/hot_search_words?latitude=' + lat + '&longitude=' + lng, function (data) {
    var html = '';
    data.on('data', function (chunk) {
      html += chunk;
    })
    data.on('end', function () {
      res.send(html);
    })
  })
})
// ShopList 商店列表页面数据
router.get('/shoplist', function (req, res) {
  console.log(req.query)
  var lati = req.query.latitude
  var long = req.query.longitude
  var limit = req.query.limit
  var cate = '&restaurant_category_ids[]=' + req.query.restaurant_category_ids
  var sort = '&order_by=' + req.query.order_by
  var mode = '&delivery_mode[]=' + req.query.delivery_mode
  var supp = '&support_ids[]=' + req.query.support_ids
  // console.log(supp)
  https.get('https://mainsite-restapi.ele.me/shopping/restaurants?' + 'latitude=' + lati + '&longitude=' + long + '&offset=0&limit=' + limit + '&extras[]=activities' + cate + sort + mode + supp, function (ress) {
    var html = '';
    ress.on('data', function (chunk) {
      html += chunk;
    })
    ress.on('end', function () {
      res.send(html);
    })
  })
})

// ShopSearch 商店搜索页面数据
router.get('/shopsearch', function (req, res) {
  console.log(req.query)
  var lati = req.query.latitude
  var long = req.query.longitude
  var offset = req.query.offset
  var limit = req.query.limit
  var cate = '&restaurant_category_ids[]=' + req.query.restaurant_category_ids
  var sort = '&order_by=' + req.query.order_by
  var mode = '&delivery_mode[]=' + req.query.delivery_mode
  var supp = '&support_ids[]=' + req.query.support_ids
  var keyword = '&keyword=' + req.query.keyword
  console.log(keyword)
  https.get('https://mainsite-restapi.ele.me/shopping/restaurants/search?search_item_type=2&latitude=' + lati + '&longitude=' + long + '&offset=' + offset + '&limit=' + limit + '&extras[]=activities' + keyword + cate + sort + mode + supp, function (ress) {
    var html = '';
    ress.on('data', function (chunk) {
      html += chunk;
    })
    ress.on('end', function () {
      res.send(html);
    })
  })
})

// mainswiper 轮播图组件数据
router.get('/swipe', function (req, res) {
  // console.log(req.query)
  var lati = req.query.latitude
  var long = req.query.longitude
  https.get('https://mainsite-restapi.ele.me/shopping/v2/entries?latitude=' + lati + '&longitude=' + long + '&templates[]=main_template', function (ress) {
    var html = '';
    ress.on('data', function (chunk) {
      html += chunk;
    })
    ress.on('end', function () {
      res.send(html);
    })
  })
})

// shop 单个商家详情页面获取数据
router.get('/shop', function (req, res) {
  var id = req.query.id
  var lat = req.query.latitude
  var lng = req.query.longitude
  console.log(id)
  https.get('https://mainsite-restapi.ele.me/shopping/restaurant/' + id + '?extras[]=activities&extras[]=albums&extras[]=license&extras[]=identification&latitude=' + lat + '&longitude=' + lng, function (ress) {
    var html = '';
    ress.on('data', function (chunk) {
      html += chunk;
    })
    ress.on('end', function () {
      res.send(html);
    })
  })
})

// shop 单个商家menu菜单数据获取
router.get('/shopmenu', function (req, res) {
  var id = req.query.id
  console.log(id)
  https.get('https://mainsite-restapi.ele.me/shopping/v2/menu?restaurant_id=' + id, function (ress) {
    var html = '';
    ress.on('data', function (chunk) {
      html += chunk;
    })
    ress.on('end', function () {
      res.send(html);
    })
  })
})

// shop 单个商家menu菜单数据获取
router.get('/shopnum', function (req, res) {
  var lng = req.query.longitude
  var lat = req.query.latitude
  https.get('https://mainsite-restapi.ele.me/shopping/v2/restaurant/category?latitude=' + lat + '&longitude=' + lng, function (ress) {
    var html = '';
    ress.on('data', function (chunk) {
      html += chunk;
    })
    ress.on('end', function () {
      res.send(html);
    })
  })
})

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/discover/v1', function (req, res) {
  // console.log(req.query);
  var lng = req.query.lng;
  // console.log(lng);
  var lat = req.query.lat;
  https.get('https://mainsite-restapi.ele.me/member/v1/discover?platform=1&block_index=0&latitude=' + lat + '&longitude=' + lng, function (data) {
    var temp = '';
    data.on('data', function (chunk) {
      temp += chunk;
    })
    data.on('end', function () {
      res.send(temp);
    })
  })
});

router.get('/discover/hotfood', function (req, res) {
  var lng = req.query.lng;
  var lat = req.query.lat;
  https.get('https://mainsite-restapi.ele.me/hotfood/v1/guess/likes?latitude=' + lat + '&longitude=' + lng + '&offset=0&limit=3&request_id=d8ff0c00-570c-4b26-9a55-0c604b558b52&tag_id=-1&columns=1', function (data) {
    var temp = '';
    data.on('data', function (chunk) {
      temp += chunk;
    })
    data.on('end', function () {
      res.send(temp);
    })
  })
});

router.get('/discover/gifts', function (req, res) {
  https.get('https://mainsite-restapi.ele.me/member/gifts/suggest', function (data) {
    var temp = '';
    data.on('data', function (chunk) {
      temp += chunk;
    })
    data.on('end', function () {
      res.send(temp);
    })
  })
});
//  存储用户名
router.get('/storage', function (req, res) {
  console.log(req.query);
  var text = req.query.text;
  var password = req.query.pw;
  MongoClient.connect(DB_CONN_STR, function (err, db) {
    if (err) {
      console.log(err);
    } else {
      var data = {
        text: text,
        password: password
      }
      console.log(data);
      // console.log('=================');
      var coll = db.collection('maizuowang');
      //  save()写入一组数据
      coll.save(data, function (err, arr) {
        if (err) {
          console.log('失败!');
        } else {
          console.log('存储成功!');
          res.send('存储成功!');
        }
      });
      db.close();
    }
  })
});
//  获取商家的分数
router.get('/ratings/scores', function (req, res) {
  var id = req.query.id;
  https.get('https://mainsite-restapi.ele.me/ugc/v2/restaurants/' + id + '/ratings/scores', function (data) {
    var temp = '';
    data.on('data', function (chunk) {
      temp += chunk;
    })
    data.on('end', function () {
      res.send(temp);
    })
  })
});

//  获取商家的满意度
router.get('/ratings/tags', function (req, res) {
  var id = req.query.id;
  https.get('https://mainsite-restapi.ele.me/ugc/v2/restaurants/' + id + '/ratings/tags', function (data) {
    var temp = '';
    data.on('data', function (chunk) {
      temp += chunk;
    })
    data.on('end', function () {
      res.send(temp);
    })
  })
});
//  获取买家的评价
router.get('/ratings/content', function (req, res) {
  var id = req.query.id;
  https.get('https://mainsite-restapi.ele.me/ugc/v2/restaurants/' + id + '/ratings?has_content=true&offset=0&limit=10', function (data) {
    var temp = '';
    data.on('data', function (chunk) {
      temp += chunk;
    })
    data.on('end', function () {
      res.send(temp);
    })
  })
});

module.exports = router;
